Public Class frmReportViewer

  Private mStrLandscapePageNum As String = String.Empty
  Private mIntExportType As CrystalDecisions.Shared.ExportFormatType

  Private Sub frmReportViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    cboChapterNum.SelectedIndex = 0
    cboExportType.SelectedIndex = 0
    mFnReadValue("LandscapePageNumber", String.Empty, "3,4,5", mStrLandscapePageNum, Application.StartupPath + "\" + CONFIG_FILE)
  End Sub

  Private Sub cmdView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdView.Click
    Dim lObjBsnsDctnry As New BsnsDctnry
    Dim lObjChangeLog As New ChangeLog

    If mPrRefreshConn() = False Then Exit Sub

    lObjBsnsDctnry.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait
    For Each lStr As String In mStrLandscapePageNum.Split(",")
      If lStr.Trim = Trim(cboChapterNum.SelectedIndex) Then
        lObjBsnsDctnry.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape
      End If
    Next

    If cboChapterNum.SelectedIndex > 0 Then
      lObjBsnsDctnry.SetParameterValue("ChptrNum", cboChapterNum.SelectedIndex)
      lObjBsnsDctnry.SetParameterValue("VerNum", txtVerNum.Text.Trim)
      lObjBsnsDctnry.SetParameterValue("Title", cboChapterNum.Text.Trim)
      lObjBsnsDctnry.SetDatabaseLogon(mPrpUserName, mPrpPassword)

      crtCrystalReportViewer.ReportSource = lObjBsnsDctnry
    Else
      lObjChangeLog.SetParameterValue("VerNum", txtVerNum.Text.Trim)
      lObjChangeLog.SetDatabaseLogon(mPrpUserName, mPrpPassword)
      crtCrystalReportViewer.ReportSource = lObjChangeLog

    End If
  End Sub

  Private Sub cmdExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExport.Click
    Dim lObjBsnsDctnry As New BsnsDctnry
    Dim lObjChangeLog As New ChangeLog
    Dim lStrFolder As String = String.Empty
    Dim lStrFileName As String = String.Empty

    If SelectFolder(lStrFolder) = False Then
      Exit Sub
    End If
    lStrFileName = lStrFolder & "\" & cboChapterNum.Text & cboExportType.Text.Split("*")(1).Split(")")(0)

    lObjBsnsDctnry.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait
    For Each lStr As String In mStrLandscapePageNum.Split(",")
      If lStr.Trim = Trim(cboChapterNum.SelectedIndex) Then
        lObjBsnsDctnry.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape
      End If
    Next

    Try
      If cboChapterNum.SelectedIndex > 0 Then
        lObjBsnsDctnry.SetParameterValue("ChptrNum", cboChapterNum.SelectedIndex)
        lObjBsnsDctnry.SetParameterValue("VerNum", txtVerNum.Text.Trim)
        lObjBsnsDctnry.SetParameterValue("Title", cboChapterNum.Text.Trim)
        lObjBsnsDctnry.SetDatabaseLogon(mPrpUserName, mPrpPassword)

        lObjBsnsDctnry.ExportToDisk(mIntExportType, lStrFileName)
      Else
        lObjChangeLog.SetParameterValue("VerNum", txtVerNum.Text.Trim)
        lObjChangeLog.SetDatabaseLogon(mPrpUserName, mPrpPassword)
        lObjChangeLog.ExportToDisk(mIntExportType, lStrFileName)
      End If
      MsgBox("Save successfully - " & lStrFileName, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    Catch ex As Exception
      MsgBox("Error", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End Try

  End Sub

  Private Sub cboExportType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboExportType.SelectedIndexChanged
    If cboExportType.Text = "PDF (*.pdf)" Then
      mIntExportType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
    ElseIf cboExportType.Text = "Excel (*.xls)" Then
      mIntExportType = CrystalDecisions.Shared.ExportFormatType.Excel
    ElseIf cboExportType.Text = "Word (*.doc)" Then
      mIntExportType = CrystalDecisions.Shared.ExportFormatType.WordForWindows
    ElseIf cboExportType.Text = "HTML (*.htm)" Then
      mIntExportType = CrystalDecisions.Shared.ExportFormatType.HTML40
    End If
  End Sub

  Private Sub cmdExportAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExportAll.Click
    Dim lObjBsnsDctnry As New BsnsDctnry
    Dim lObjChangeLog As New ChangeLog
    Dim lStrFolder As String = String.Empty
    Dim lStrFileName As String = String.Empty

    If SelectFolder(lStrFolder) = False Then
      Exit Sub
    End If

    Try
      For Each lStrReport As String In cboChapterNum.Items
        Dim lIntSelectedIndex As Integer = cboChapterNum.Items.IndexOf(lStrReport)

        lStrFileName = lStrFolder & "\" & lStrReport & cboExportType.Text.Split("*")(1).Split(")")(0)
        lObjBsnsDctnry.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait
        For Each lStr As String In mStrLandscapePageNum.Split(",")
          If lStr.Trim = Trim(lIntSelectedIndex.ToString) Then
            lObjBsnsDctnry.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape
          End If
        Next
        If lIntSelectedIndex > 0 Then
          lObjBsnsDctnry.SetParameterValue("ChptrNum", lIntSelectedIndex)
          lObjBsnsDctnry.SetParameterValue("VerNum", txtVerNum.Text.Trim)
          lObjBsnsDctnry.SetParameterValue("Title", lStrReport)
          lObjBsnsDctnry.SetDatabaseLogon(mPrpUserName, mPrpPassword)

          lObjBsnsDctnry.ExportToDisk(mIntExportType, lStrFileName)
        Else
          lObjChangeLog.SetParameterValue("VerNum", txtVerNum.Text.Trim)
          lObjChangeLog.SetDatabaseLogon(mPrpUserName, mPrpPassword)
          lObjChangeLog.ExportToDisk(mIntExportType, lStrFileName)
        End If

      Next

      MsgBox("Save successfully in folder - " & lStrFolder, MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub

    Catch ex As Exception
      MsgBox("Error", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End Try

  End Sub

End Class